﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace KaiXin001er.UTIL
{
    public  class StringHelper
    {
        public static string ReadXmlSchema(string schemaName,string src,ref int  pos)
        {
            return getMiddleString("<"+schemaName+">","</"+schemaName+">",src,ref pos);
        }
        public static string UrlEncode(string str)
        {
            return HttpUtility.UrlEncode(str);
        }
        public static int IndexOf(string str, string querystr)
        {
            return str.IndexOf(querystr);
        }
        public static bool Contains(string querystr, string src)
        {
            return src.Contains(querystr);
        }
        public static void seek(string src, string dest,ref int pos)
        {
            int seekindex=src.IndexOf(dest, pos);
            if (seekindex < 0) pos=seekindex;
            else
            {
                pos = seekindex + dest.Length;
            }
        }
        public static string getLastString(string src,int pos)
        {
            return src.Substring(pos);
        }
        public static string getMiddleString(string startstr, string endstr, string srcstr)
        {
            int pos = 0;
            return getMiddleString(startstr, endstr, srcstr, ref pos);
        }
        public static string getMiddleString(string startstr, string endstr, string srcstr, ref int pos)
        {
            int startIndex = srcstr.IndexOf(startstr, pos);
            if (startIndex == -1) return null;
            //从这个开始处找
            int endIndex = srcstr.IndexOf(endstr, startIndex+startstr.Length);
            if (endIndex == -1) return null;
            if (startIndex >= endIndex) return null;
            string retstr = srcstr.Substring(startIndex+startstr.Length, endIndex - startIndex-startstr.Length);
            pos = endIndex + endstr.Length-1;
            return retstr.Trim();
        }
        public static string[] splitByString(string splitstring , string srcstring)
        {
            string[] strs=srcstring.Split(new string[]{splitstring},StringSplitOptions.RemoveEmptyEntries);
           
            for (int i = 0; i < strs.Length;i++ )
            {
                strs[i] = strs[i].Trim();
            }
            return strs;
        }

        public static string ReadXmlSchema(string schemaName, string src)
        {
            int pos = 0;
            return ReadXmlSchema( schemaName ,src,ref  pos);
        }
    }
}
